﻿using Estsh.Core.Wms.IServices;
using Estsh.Core.Model.Result;
using Estsh.Core.Util;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Estsh.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Estsh.Core.Controllers;
using Newtonsoft.Json;
using Estsh.Core.Model.Extend.Models;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.PickingList
{
    [Route("wms/[controller]")]
    [ApiController]
    public class InpdlineController : Controller
    {
        private IInpdlineService service;

        // GET: LoginController
        public InpdlineController(IInpdlineService _service)
        {
            service = _service;
        }

        [HttpPost("CheckMoveHeader")]
        public IActionResult CheckMoveHeader(Newtonsoft.Json.Linq.JObject jobj)
        {
            string onLine = jobj["onLine"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsMoveHeader> obj = service.CheckMoveHeader(onLine);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckOnlineMoveHeader")]
        public IActionResult CheckOnlineMoveHeader(Newtonsoft.Json.Linq.JObject jobj)
        {
            
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string onLine = jobj["onLine"].ToString();
                string carton_no = jobj["cartonno"].ToString();
                List<WmsMoveHeader> obj = service.CheckOnlineMoveHeader(onLine,carton_no);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            { 
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMoveDetail")]
        public IActionResult CheckMoveDetail(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsMoveDetail_Extend> obj = service.CheckMoveDetail(receving);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMoveDetail2")]
        public IActionResult CheckMoveDetail2(Newtonsoft.Json.Linq.JObject jobj)
        {
            string receving = jobj["receving"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsMoveDetail> obj = service.CheckMoveDetail2(receving);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("UpdatePick")]
        public IActionResult UpdatePick(Newtonsoft.Json.Linq.JObject jobj)
        {
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string cartonNo = jobj["cartonNo"].ToString();
            string isSplit = jobj["isSplit"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.UpdatePick(loginId, orderNo, cartonNo, isSplit);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("BatchingCompleted")]
        public IActionResult BatchingCompleted(Newtonsoft.Json.Linq.JObject jobj)
        {
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.BatchingCompleted(loginId, orderNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckLocateStatus")]
        public IActionResult CheckLocateStatus(Newtonsoft.Json.Linq.JObject jobj)
        {
            string locate = jobj["locate"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckLocateStatus(locate, orderNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("UpdatePickOnline")]
        public IActionResult UpdatePickOnline(Newtonsoft.Json.Linq.JObject jobj)
        {
            string locate = jobj["locate"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string loginId = jobj["loginId"].ToString();
            string cartonNo = jobj["cartonNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.UpdatePickOnline(loginId, orderNo, cartonNo, locate);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }


        [HttpPost("GetPullingOrderList")]
        public IActionResult GetPullingOrderList(Newtonsoft.Json.Linq.JObject jobj)
        {
            string Line = jobj["Line"].ToString();
            string MaterialType = jobj["MaterialType"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsPullingOrder> obj = service.GetPullingOrderList(Line, MaterialType);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("GetPullingOrderDetails")]
        public IActionResult GetPullingOrderDetails(Newtonsoft.Json.Linq.JObject jobj)
        {
            string orderNo = jobj["orderNo"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<WmsPullingOrderDetail> obj = service.GetPullingOrderDetails(orderNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckRK")]
        public IActionResult CheckRK(Newtonsoft.Json.Linq.JObject jobj)
        {
            string RKNO = jobj["RKNO"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string loginId = jobj["loginId"].ToString();
            string isUpdate = jobj["isUpdate"].ToString();
            string UpdateNum = jobj["UpdateNum"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckRK(RKNO, orderNo, loginId, isUpdate, UpdateNum);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMaterial")]
        public IActionResult CheckMaterial(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string updateNum = jobj["updateNum"].ToString();
            string loginId = jobj["loginId"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckMaterial(cartonNo, orderNo, updateNum, loginId);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("CheckMaterial2")]
        public IActionResult CheckMaterial2(Newtonsoft.Json.Linq.JObject jobj)
        {
            string cartonNo = jobj["cartonNo"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string isUpdate = jobj["isUpdate"].ToString();
            string loginId = jobj["loginId"].ToString();
            string UpdateNum = jobj["UpdateNum"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.CheckMaterial(cartonNo, orderNo, isUpdate, loginId, UpdateNum);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        [HttpPost("SetMoveOrderClose")]
        public IActionResult SetMoveOrderClose(Newtonsoft.Json.Linq.JObject jobj)
        {
            string order = jobj["order"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                string obj = service.SetMoveOrderClose(order);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }



        #region 生产领料（一步）


        [HttpPost("CheckRackOrcartNo")]
        public IActionResult CheckRackOrcartNo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string cartonNo = jobj["cartonNo"].ToString();
            string isSplit = jobj["isSplit"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckRackOrcartNo(loginId, orderNo, cartonNo, isSplit);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }



        /// <summary>
        /// 输入拆分数量
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckSplitQty")]
        public IActionResult CheckSplitQty(Newtonsoft.Json.Linq.JObject jobj)
        {
            string loginId = jobj["loginId"].ToString().Trim();
            string rackOrCartNo = jobj["rackOrCartNo"].ToString().Trim();
            string cartNo = jobj["cartonNo"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string SplitQty = jobj["SplitQty"].ToString().Trim();
            WmsResponseResult_Extend result = new WmsResponseResult_Extend();
            try
            {
                SetObjectDetail obj = service.CheckSplitQty(loginId,  cartNo,  orderNo,  SplitQty,  rackOrCartNo);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }




        
        /// <summary>
        /// 扫描目的周转箱
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("CheckDestCartonOrRack")]
        public IActionResult CheckDestCartonOrRack(Newtonsoft.Json.Linq.JObject jobj)
        {
            string destRackNo = jobj["destRackNo"].ToString().Trim();
            string cartonNo = jobj["cartonNo"].ToString().Trim();
            string rackOrCartNo = jobj["rackOrCartNo"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string SplitQty = jobj["SplitQty"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckDestCartonOrRack(orderNo, rackOrCartNo, cartonNo, destRackNo, SplitQty, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }




        /// <summary>
        /// 整箱领料
        /// </summary>
        /// <param name="jobj"></param>
        /// <returns></returns>
        [HttpPost("FullBoxPicking")]
        public IActionResult FullBoxPicking(Newtonsoft.Json.Linq.JObject jobj)
        {
            string rackOrCartNo = jobj["rackOrCartNo"].ToString().Trim();
            string orderNo = jobj["orderNo"].ToString().Trim();
            string loginId = jobj["loginId"].ToString().Trim();

            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.FullBoxPicking(orderNo, rackOrCartNo, loginId);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }



        #endregion


        #region 领料上线（二步）

        [HttpPost("CheckOnlineRackOrcartNo")]
        public IActionResult CheckOnlineRackOrcartNo(Newtonsoft.Json.Linq.JObject jobj)
        {
            string loginId = jobj["loginId"].ToString();
            string orderNo = jobj["orderNo"].ToString();
            string cartonNo = jobj["cartonNo"].ToString();
            string locate = jobj["locate"].ToString();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                SetObjectDetail obj = service.CheckOnlineRackOrcartNo(loginId, orderNo, cartonNo, locate);

                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        #endregion


    }
}
